﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace emulib
{
    /// <summary>
    /// Provides an IMmoServ object with access to file system safely
    /// </summary>
    public class WorkDirectory
    {
        String fullPath;
        public WorkDirectory(ServerInfo servInfo)
        {
            fullPath = String.Format("{0}\\servers\\{1}\\", 
                Directory.GetCurrentDirectory(), 
                servInfo.serverGuid.ToString());
            Debug.WriteLine(fullPath);
            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
        }
        public void WriteFile(String filename, String text)
        {
            WriteFile(filename, Encoding.Unicode.GetBytes(text));
        }
        public void WriteFile(String filename, Stream data)
        {
            Byte[] buf = new Byte[(Int32)data.Length];
            data.Read(buf, 0, (Int32)data.Length);
            WriteFile(filename, buf);
        }
        public void WriteFile(String filename, Byte[] data)
        {
            FileStream fs = new FileStream(fullPath + filename, FileMode.Create);
            fs.Write(data, 0, data.Length);
            fs.Close();
        }
        /// <summary>
        /// Get the full path to a specific file or directory for the IMmoServ object
        /// </summary>
        /// <param name="name">File or directory to get full path for</param>
        /// <returns>Full disc path to file or directory given</returns>
        public String GetFullPath(String name)
        {
            return fullPath + name;
        }
    }
}
